<template>
  <div class="top-header">
    <div class="logo-container">
      <img src="../assets/img/系统图标.png" alt="系统图标" class="logo-icon" />
      <h1 class="system-title">招标文件智析系统</h1>
    </div>
    <div class="right-section">
      <div class="notification-icon" @mouseenter="showNotificationPreview" @mouseleave="hideNotificationPreview">
        <img src="../assets/icons/通知.svg" alt="通知" />
        <div class="notification-count" v-if="unreadCount > 0">{{ displayCount }}</div>
        
        <!-- 招标文件名称提示框 -->
        <div class="bidding-file-tooltip" v-if="hoveredItem !== null && tooltipContent" :style="tooltipStyle">
          {{ tooltipContent }}
        </div>
        
        <!-- 通知预览弹窗 -->
        <div class="notification-preview" v-if="showPreview" @mouseenter="keepPreviewVisible" @mouseleave="hideNotificationPreview">
          <div class="preview-header">
            <span>消息通知 ({{ unreadCount }})</span>
            <button class="mark-all-read-btn" @click="markAllAsRead">一键已读</button>
          </div>
          <div class="preview-body">
            <div 
              class="preview-item" 
              v-for="item in unreadNotifications" 
              :key="item.id"
              @mouseenter="showBiddingFile(item, $event)"
              @mouseleave="hideBiddingFile"
            >
              <div class="preview-content">
                <div class="preview-title">{{ item.title }}</div>
                <div class="preview-time">{{ item.time }}</div>
              </div>
            </div>
            <div class="empty-preview" v-if="unreadNotifications.length === 0">
              <p>暂无未读通知</p>
            </div>
          </div>
          <div class="preview-footer">
            <button class="view-all-btn" @click="viewAllNotifications">查看全部</button>
          </div>
        </div>
      </div>
      <div class="user-info" @click="navigateTo('/user-settings')">
        <img src="../assets/icons/个人.svg" alt="用户" class="user-avatar" />
        <span>User</span>
      </div>
    </div>
  </div>
</template>

<script>
import { ref, computed, inject } from 'vue';
import { useRouter } from 'vue-router';

export default {
  name: 'TopHeader',
  setup() {
    const router = useRouter();
    const showPreview = ref(false);
    const hoveredItem = ref(null);
    const tooltipContent = ref('');
    const tooltipStyle = ref({
      left: '0px',
      top: '0px'
    });
    
    // 注入全局通知数据
    const globalNotifications = inject('globalNotifications');
    
    // 页面跳转函数
    const navigateTo = (path) => {
      router.push(path);
    };

    // 未读通知数量
    const unreadCount = computed(() => globalNotifications.unreadCount.value);
    
    // 未读通知列表
    const unreadNotifications = computed(() => {
      return globalNotifications.unreadNotifications.value.slice(0, 6);
    });
    
    // 显示的数字
    const displayCount = computed(() => {
      return unreadCount.value > 99 ? '99+' : unreadCount.value;
    });
    
    // 显示通知预览
    const showNotificationPreview = () => {
      showPreview.value = true;
    };
    
    // 保持预览可见
    const keepPreviewVisible = () => {
      showPreview.value = true;
    };
    
    // 隐藏通知预览
    const hideNotificationPreview = () => {
      showPreview.value = false;
      hoveredItem.value = null;
    };
    
    // 显示/隐藏招标文件名称
    const showBiddingFile = (item, event) => {
      hoveredItem.value = item.id;
      tooltipContent.value = item.biddingFile;
      
      const rect = event.currentTarget.getBoundingClientRect();
      tooltipStyle.value = {
        left: (rect.left - 180) + 'px',
        top: (rect.top + rect.height / 2) + 'px'
      };
    };
    
    const hideBiddingFile = () => {
      hoveredItem.value = null;
      tooltipContent.value = '';
    };
    
    // 查看所有通知
    const viewAllNotifications = () => {
      router.push('/notification');
      showPreview.value = false;
    };
    
    // 标记所有通知为已读
    const markAllAsRead = (event) => {
      event.stopPropagation();
      globalNotifications.markAllAsRead();
    };
    
    return {
      showPreview,
      unreadCount,
      displayCount,
      unreadNotifications,
      hoveredItem,
      tooltipContent,
      tooltipStyle,
      navigateTo,
      showNotificationPreview,
      hideNotificationPreview,
      keepPreviewVisible,
      showBiddingFile,
      hideBiddingFile,
      viewAllNotifications,
      markAllAsRead,
    };
  }
}
</script>

<style scoped>
.top-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 20px;
  height: 60px;
  background-color: transparent;
}

.logo-container {
  display: flex;
  align-items: center;
}

.logo-icon {
  width: 40px;
  height: 40px;
  margin-right: 10px;
}

.system-title {
  color: #13227A;
  font-size: 20px;
  font-weight: bold;
  margin: 0;
}

.right-section {
  display: flex;
  align-items: center;
}

.notification-icon {
  position: relative;
  margin-right: 25px;
  cursor: pointer;
}

.notification-icon img, .user-avatar {
  width: 30px;
  height: 30px;
}

.notification-count {
  position: absolute;
  top: -8px;
  right: -8px;
  min-width: 18px;
  height: 18px;
  background-color: red;
  border-radius: 9px;
  color: white;
  font-size: 12px;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 0 4px;
}

.user-info {
  display: flex;
  align-items: center;
  cursor: pointer;
}

.user-avatar {
  margin-right: 12px;
}

/* 通知预览弹窗 */
.notification-preview {
  position: absolute;
  top: 35px;
  right: 0;
  width: 300px;
  background-color: white;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.15);
  z-index: 1000;
  overflow: visible;
}

.preview-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 15px;
  border-bottom: 1px solid #e0e0e0;
}

.mark-all-read-btn {
  padding: 4px 8px;
  background-color: #009688;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 12px;
}

.preview-body {
  max-height: 400px;
  overflow-y: auto;
  overflow-x: visible;
  position: relative;
}

.preview-item {
  padding: 10px 15px;
  border-bottom: 1px solid #f0f0f0;
  cursor: pointer;
  position: relative;
  overflow: visible;
}

.preview-item:hover {
  background-color: #f5f7fa;
}

.preview-content {
  display: flex;
  flex-direction: column;
}

.preview-title {
  font-size: 14px;
  margin-bottom: 5px;
  color: #303133;
}

.preview-time {
  font-size: 12px;
  color: #909399;
}

.bidding-file-tooltip {
  position: fixed;
  transform: translateY(-50%);
  width: 160px;
  background-color: #ffffff;
  color: #303133;
  padding: 10px;
  border-radius: 6px;
  z-index: 9999;
  font-size: 13px;
  box-shadow: 0 3px 10px rgba(0, 0, 0, 0.15);
  text-align: left;
  border: 1px solid #e0e0e0;
  pointer-events: none;
  transition: opacity 0.2s ease;
}

.bidding-file-tooltip::after {
  content: '';
  position: absolute;
  right: -6px;
  top: 50%;
  transform: translateY(-50%);
  width: 0;
  height: 0;
  border-top: 6px solid transparent;
  border-bottom: 6px solid transparent;
  border-left: 6px solid #ffffff;
}

.empty-preview {
  padding: 20px;
  text-align: center;
  color: #909399;
}

.preview-footer {
  padding: 10px;
  text-align: center;
  border-top: 1px solid #e0e0e0;
}

.view-all-btn {
  padding: 6px 12px;
  background-color: #f0f0f0;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  color: #606266;
  font-size: 14px;
}

.view-all-btn:hover {
  background-color: #e0e0e0;
}
</style>